import { createSlice, PayloadAction } from "@reduxjs/toolkit"

export type PageInfoType = {
    title: string
    desc?: string
    js?: string
    css?: string
    isPublished?: boolean
}

const INIT_STATE: PageInfoType = {
    title: "",
    desc: "",
    js: "",
    css: ""
    // 默认初始值为空
}

const pageInfoSlice = createSlice({
    name: "pageInfo",
    initialState: INIT_STATE,
    reducers: {
        // 重置页面信息（其实就是初始化）
        resetPageInfo(state: PageInfoType, action: PayloadAction<PageInfoType>) {
            return action.payload
        },

        // 修改标题
        changePageTitle(state: PageInfoType, action: PayloadAction<string>) {
            // state 就等同于 INIT_STATE,只不过值可能不一样
            state.title = action.payload
        }
    }
})

export const { resetPageInfo, changePageTitle } = pageInfoSlice.actions
export default pageInfoSlice.reducer
